package org.test.client.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.test.client.ui.component.BreadcrumbNav;
import org.test.client.ui.component.MyBasePanel;
import org.test.client.ui.content.ChangePanelListener;
import org.test.client.ui.left.LeftMenuBar;
import org.test.client.ui.left.LeftMenuItem;
import org.test.client.ui.left.leftMenu;
import org.test.client.ui.top.TopPanel;
import org.test.client.ui.util.UIConstants;
import org.test.client.ui.util.UIUtil;

public class MainPanel extends JPanel{

	private MainPanel thisMainPanel = this;
	private MainApplication frame = null;
	private TopPanel topPanel = null;
	private JPanel centerPanel = null;
	
//	private JPanel contentContainer = null;
//	private GridBagLayout contentLayout = null;
//	private GridBagConstraints gbs= new GridBagConstraints();//定义一个GridBagConstraints，
	
	private LeftMenuBar leftMenuBar = null;
	private BreadcrumbNav bbNav = null;
	private ContentMain contentMain = null;
	
	
	public MainPanel(MainApplication frame) {
		this.frame = frame;
		
		doUIInit();
		doListenerInit();
	}
	
	
	public void doUIInit(){
		topPanel = new TopPanel(thisMainPanel);
		
		leftMenuBar = new LeftMenuBar(this);
		LeftMenuItem dashBoard = new LeftMenuItem("DashBoard", "fa-home").doInit();
		LeftMenuItem uiFeature = new LeftMenuItem("UI Feature", "fa-eye").doInit();

		LeftMenuItem formsDemo = new LeftMenuItem("Forms" ,"fa-credit-card").doInit();
		LeftMenuItem chartDemo = new LeftMenuItem("Charts" ,"fa-list").doInit();
		LeftMenuItem tableDemo = new LeftMenuItem("Tables" ,"fa-align-justify").doInit();
		LeftMenuItem itemw = new LeftMenuItem("Calendar" ,"fa-calendar").doInit();

		LeftMenuItem child2 = new LeftMenuItem("Child Menu2" ,2).doInit();
		LeftMenuItem item3 = new LeftMenuItem("According Menu", "fa-plus").doInit();
		item3.addChild(child2);
		for(int i=0;i<25;i++){
			LeftMenuItem child1 = new LeftMenuItem("Child Menu"+i ,2).doInit();
			item3.addChild(child1);
		}
		leftMenu menu1 = new leftMenu(item3);
		
		
		LeftMenuItem according = new LeftMenuItem("According Menu", "fa-plus").doInit();
		item3.addChild(child2);
		for(int i=0;i<3;i++){
			LeftMenuItem child1 = new LeftMenuItem("Child Menu"+i ,2).doInit();
			according.addChild(child1);
		}
		leftMenu menu2 = new leftMenu(according);
		
		leftMenuBar.addLabel("MAIN");
		leftMenuBar.addItem(dashBoard);
		leftMenuBar.addItem(uiFeature);
		leftMenuBar.addItem(formsDemo);
		leftMenuBar.addLabel("SAMPLE SECTION");
		leftMenuBar.addItem(chartDemo);
		leftMenuBar.addItem(tableDemo);
		leftMenuBar.addItem(menu2);
		leftMenuBar.addItem(itemw);
		leftMenuBar.addItem(menu1);
		leftMenuBar.doInit();
		
		dashBoard.addClickListener(new ChangePanelListener(this, dashBoard, "DashBoard"));
		uiFeature.addClickListener(new ChangePanelListener(this, uiFeature, "UIFeature"));
		formsDemo.addClickListener(new ChangePanelListener(this, formsDemo, "FormsDemo"));
		chartDemo.addClickListener(new ChangePanelListener(this, formsDemo, "ChartDemo"));
		tableDemo.addClickListener(new ChangePanelListener(this, tableDemo, "TableDemo"));
		
		
		bbNav = new BreadcrumbNav(thisMainPanel).doInit();
		contentMain = new ContentMain(thisMainPanel);
		JPanel contentContainer = new JPanel(new BorderLayout());
		centerPanel = new JPanel(new BorderLayout());
		JPanel placeHolder = new JPanel();
		placeHolder.setPreferredSize(new Dimension(UIConstants.leftMenuAndMainGap,0));
		contentContainer.add(placeHolder,BorderLayout.WEST);
		contentContainer.add(bbNav,BorderLayout.NORTH);
		contentContainer.add(contentMain);
		
		centerPanel = new JPanel(new BorderLayout());
		JPanel placeHolder2 = new JPanel();
		placeHolder2.setPreferredSize(new Dimension(0,UIConstants.topAndMainGap));
		centerPanel.add(placeHolder2,BorderLayout.NORTH);
		
		
		JScrollPane leftScroll = new JScrollPane();
		leftScroll.getVerticalScrollBar().setUnitIncrement(15);
		leftScroll.setViewportView(leftMenuBar);
		centerPanel.add(leftScroll,BorderLayout.WEST);
		
//		centerPanel.add(leftMenuBar,BorderLayout.WEST);
		centerPanel.add(contentContainer);
		
		
		this.setLayout(new BorderLayout());
		this.add(topPanel,BorderLayout.NORTH);
		this.add(centerPanel);
		
		leftMenuBar.setSelectedItem(formsDemo);
		changePanel("FormsDemo");
		
	}
	
	public void changePanel(String key){
		try {
			contentMain.changePanel(key);
			bbNav.replaceLast(key, null);
			bbNav.repaint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public void doListenerInit(){
		
	}
	
	public MainApplication getMainFrame(){
		return frame;
	}


	public TopPanel getTopPanel() {
		return topPanel;
	}

	public LeftMenuBar getLeftMenuBar() {
		return leftMenuBar;
	}


	public BreadcrumbNav getBbNav() {
		return bbNav;
	}



	public ContentMain getContentMain() {
		return contentMain;
	}

	public void setContentMain(ContentMain contentMain) {
		this.contentMain = contentMain;
	}
	
}
